package starComponent.elements;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RadialGradientPaint;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import starComponent.Listeners.ListenerThumb;
import starComponent.controls.ObservableThumb;
import starComponent.controls.ObserverThumb;
import tuio.AcceleratedTouchEvent;
import tuio.TCurAndPoint;
import tuio.components.LabelTouch;

/**
 * @author Charles Perin
 * 12 avr. 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public abstract class A_Thumb extends LabelTouch implements ObservableThumb{

	protected static final long serialVersionUID = 5604679035803329636L;

	protected static final int THUMB_SIDE = 40;

	protected A_Branch branch;

	protected int xPos, yPos;
	protected boolean isSelected;

	protected Point center;

	protected Color[] thumbColor = new Color[]{Color.BLACK, new Color(200,200,200)};
	protected Color[] thumbSelectedColor = new Color[]{Color.RED, new Color(255,200,200)};

	protected double value;

	protected double blockedValue;

	protected ArrayList<ObserverThumb> listObserver = new ArrayList<ObserverThumb>();

	public A_Thumb(A_Branch branch){
		this.branch = branch;
		ListenerThumb listener = new ListenerThumb(this);
		addMouseListener(listener);
		addMouseMotionListener(listener);
	}


	@Override
	public void paint(Graphics g){
		Graphics2D g2d = (Graphics2D)g;

		if(isSelected)g2d.setPaint(new RadialGradientPaint(new Point(this.getWidth()/2, this.getHeight()/2), 10, new float[]{0.2f, 1f}, thumbSelectedColor));
		else g2d.setPaint(new RadialGradientPaint(new Point(this.getWidth()/2, this.getHeight()/2), 10, new float[]{0.2f, 1f}, thumbColor));
		g2d.fillOval(this.getWidth()/2-THUMB_SIDE/4, this.getHeight()/2-THUMB_SIDE/4, THUMB_SIDE/2, THUMB_SIDE/2);
	}

	public int getxPos() {
		return xPos;
	}

	public void setxPos(int xPos) {
		this.xPos = xPos;
	}

	public int getyPos() {
		return yPos;
	}

	public void setyPos(int yPos) {
		this.yPos = yPos;
	}
	
	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
	}

	public boolean isSelected() {
		return isSelected;
	}
	
	public double getValue() {
		return value;
	}

	public Color[] getThumbColor() {
		return thumbColor;
	}

	public void setThumbColor(Color[] thumbColor) {
		this.thumbColor = thumbColor;
	}

	public Color[] getThumbSelectedColor() {
		return thumbSelectedColor;
	}

	public void setThumbSelectedColor(Color[] thumbSelectedColor) {
		this.thumbSelectedColor = thumbSelectedColor;
	}

	public Point getCenter() {
		return center;
	}


	public double getBlockedValue() {
		return blockedValue;
	}

	
	@Override
	public void addObserverThumb(ObserverThumb obs) {
		listObserver.add(obs);
	}

	@Override
	public void updateObserverThumb() {
		for(ObserverThumb obs : listObserver){
			obs.update(this);
		}
	}

	@Override
	public void deleteObserverThumb() {
		listObserver = new ArrayList<ObserverThumb>();
	}

	@Override
	public void touchDragged(TCurAndPoint tCurAndPoint) {
		AcceleratedTouchEvent evt = new AcceleratedTouchEvent(this, MouseEvent.MOUSE_DRAGGED, 0, InputEvent.BUTTON1_MASK, tCurAndPoint.getLocalPoint().x, tCurAndPoint.getLocalPoint().y, 1, false, tCurAndPoint.gettCur().getMotionSpeed());
		dispatchEvent(evt);
	}

	public A_Branch getBranch() {
		return branch;
	}


	public void checkNewPosition(int x, int y) {
		
	}

	
}
